﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiGeSIB.GUI.Management;
using SiGeSIB.GUI.Common;

namespace SiGeSIB.GUI
{
	public partial class FMain : Form
	{
		public FMain()
		{
			InitializeComponent();
			uFinancesControl.Visible = true;
			uManagementControl.Visible = false;
			uSecurityControl.Visible = false;
			uRestorationBackupControl.Visible = false;
		}

		private void btnFinancial_Click(object sender, EventArgs e)
		{
			uFinancesControl.Visible = true;
			uManagementControl.Visible = uSecurityControl.Visible = uRestorationBackupControl.Visible = false;
		}

		private void btnAdministrative_Click(object sender, EventArgs e)
		{
			uManagementControl.Visible = true;
			uFinancesControl.Visible = uSecurityControl.Visible = uRestorationBackupControl.Visible = false;
		}

		private void btnSecurity_Click(object sender, EventArgs e)
		{
			uSecurityControl.Visible = true;
			uFinancesControl.Visible = uManagementControl.Visible = uRestorationBackupControl.Visible = false;
		}

		private void btnRestorationBackup_Click(object sender, EventArgs e)
		{
			uRestorationBackupControl.Visible = true;
			uFinancesControl.Visible = uManagementControl.Visible = uSecurityControl.Visible = false;
		}

		public void CreateTab(ConflictableForm f)
		{
			bool alreadyCreated = false;
			TabPage instance = null;

			// Verifica se tem alguma aba que conflita com a aba a ser aberta
			foreach (TabPage page in tabControl.TabPages)
				if (page.Text == f.Text)
				{
					if (f.ConflictsWith((ConflictableForm)page.Tag))
					{
						alreadyCreated = true;
						instance = page;
						break;
					}
				}

			// Se não conflita
			if (!alreadyCreated)
			{
				TabPage t = new TabPage(f.Text);
				//t.
				f.Dock = DockStyle.Fill;
				t.Controls.Add(f);
				t.Tag = f;

				int lastSelectedIndex = tabControl.SelectedIndex;

				f.RequestClose += new EventHandler<EventArgs<DialogResult>>(
					(obj, args) =>
					{
						tabControl.SelectedIndex = lastSelectedIndex;
						tabControl.TabPages.Remove(t);
						t.Dispose();
					});

				tabControl.TabPages.Add(t);
				instance = t;
			}

			tabControl.SelectedTab = instance;
		}
	}
}
